package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.qo.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/department")
public class DepartmentController {


    private IDepartmentService departmentService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }
    @RequiredPermission(name="部门页面",expression="department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject queryObject){
        model.addAttribute("PageInfo",departmentService.query(queryObject));
        return "/department/list";
    }
    @RequiredPermission(name="部门删除",expression="department:delete")
    @RequestMapping("/delete")
    public String delete(Long id ){
        departmentService.delete(id);
        return "redirect:/department/list";
    }

    //去到新增页面
    @RequiredPermission(name="部门新增/编辑",expression="department:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id ,Model model ){
        if(id != null){
            model.addAttribute("department",departmentService.selectByPrimaryKey(id));
        }
        return  "/department/input";
    }
    @RequiredPermission(name="部门新增/编辑",expression="department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if(department.getId() != null){
            departmentService.update(department);
        }else {
            departmentService.insert(department);
        }
        return "redirect:/department/list";
    }

}
